package system.task.listener;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.context.ApplicationEventPublisher;
import system.task.Task;
import system.task.TaskManager;
import system.task.defined.TaskRunStatus;
import system.task.errors.TaskException;
import system.task.events.TaskAddEvent;
import system.task.events.TaskDeleteEvent;
import system.task.utils.TaskUtil;

import java.util.Date;

@Slf4j
public class ScheListener implements SchedulerListener {

    private final ApplicationEventPublisher eventPublisher;
    private final TaskManager taskManager;

    public ScheListener(TaskManager taskManager, ApplicationEventPublisher eventPublisher) {
        this.taskManager = taskManager;
        this.eventPublisher = eventPublisher;
    }

    @Override
    public void jobScheduled(Trigger trigger) {
        JobKey jobKey = trigger.getJobKey();
        //
        if (!trigger.getJobDataMap().containsKey(TaskUtil.TASK_TRIGGER_MANUAL)) {
            Date nextTime = trigger.getNextFireTime();
            if (nextTime != null) {
                try {
                    Task task = taskManager.getTask(jobKey.getName());
                    if (task != null) {
                        task.setNextTime(nextTime);
                    }
                } catch (TaskException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    @Override
    public void jobUnscheduled(TriggerKey triggerKey) {
        try {
            Task task = taskManager.getTask(triggerKey.getName());
            if (task != null) {
                //task.setLoadTime(new Date());
                task.setNextTime(null);
            }
        } catch (TaskException e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void triggerFinalized(Trigger trigger) {

    }

    @Override
    public void triggerPaused(TriggerKey triggerKey) {

    }

    @Override
    public void triggersPaused(String s) {

    }

    @Override
    public void triggerResumed(TriggerKey triggerKey) {

    }

    @Override
    public void triggersResumed(String s) {

    }

    @SneakyThrows
    @Override
    public void jobAdded(JobDetail jobDetail) {
        JobKey jobKey = jobDetail.getKey();
        Task task = taskManager.getTask(jobKey.getName());
        //
        task.setLoadTime(new Date());
        task.setRunStatus(TaskRunStatus.READY);
        task.setRunTimeStart(null);
        task.setRunTimeEnd(null);
        task.setRunRemarks("");
        task.setNextTime(null);
        task.setPrevTime(null);
        task.setScheTime(null);
        //
        this.eventPublisher.publishEvent(new TaskAddEvent(task));
    }

    @Override
    public void jobDeleted(JobKey jobKey) {
        String name = jobKey.getName();
        this.eventPublisher.publishEvent(new TaskDeleteEvent(name));
    }

    @Override
    public void jobPaused(JobKey jobKey) {
    }

    @Override
    public void jobsPaused(String s) {

    }

    @Override
    public void jobResumed(JobKey jobKey) {
    }

    @Override
    public void jobsResumed(String s) {

    }

    @Override
    public void schedulerError(String s, SchedulerException e) {
        log.error(s, e);
    }

    @Override
    public void schedulerInStandbyMode() {

    }

    @Override
    public void schedulerStarted() {

    }

    @Override
    public void schedulerStarting() {

    }

    @Override
    public void schedulerShutdown() {

    }

    @Override
    public void schedulerShuttingdown() {

    }

    @Override
    public void schedulingDataCleared() {

    }
}
